package cn.smbms.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.pojo.User;
import cn.smbms.service.UserService;
import cn.smbms.tools.Constants;

@Controller
public class LoginController {
	@Resource
	private UserService usi;
	
	@RequestMapping("login.html")
	public String login(){
		return "login";
	}
	
	@RequestMapping("dologin.html")
	public String doLogin(@RequestParam String userCode,
			@RequestParam String userPassword, HttpSession session,
			HttpServletRequest request) {
		User user = usi.userCode(userCode);
		if (user != null) {
			user = null;
			user = usi.login(userCode, userPassword);
			if (user != null) {
				session.setAttribute(Constants.USER_SESSION, user);
				return "redirect:/user/main.html";
			} else {
				throw new RuntimeException("密码输入错误！");
			}
		} else {
			throw new RuntimeException("用户名不存在！");
		}
	}
	
	@RequestMapping("logout.html")
	public String logout(HttpSession session){
		session.removeAttribute(Constants.USER_SESSION);
		return "login";
	}
}
